#include <iostream>
#include <cstring>
#include <unistd.h>
using namespace std;
// 父进程 写
// 子进程 读
#define N 1024
int main()
{
    int piped[2] = {0};
    int ret = pipe(piped);
    pid_t pid = fork();
    if (pid == -1)
    {
        perror("fork");
        return 1;
    }
    else if (pid == 0)
    {
        char buffer[N];
        close(piped[1]);
        int ret1 = read(piped[0], buffer, sizeof(buffer));
        if(ret1==-1){
            perror("read");
            return 1;
        }
        printf("%s\n",buffer);
        close(piped[0]);
        exit(0);
    }
    else{
        char buffer[N];
        close(piped[0]);
        int ret2=write(piped[1],"CBRcbrcbr",9);
        if(ret2==-1){
            perror("write");
            return 1;
        }
        printf("父进程写的长度：%d\n",ret2);
        close(piped[1]);
    }
    return 0;
}